﻿using MongoDB.Driver;
using PolyNoteServer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PolyNoteServer.Services
{
    public class NoteService
    {
        private readonly IMongoCollection<Note> _notes;

        public NoteService(INotesDatabaseSettings settings)
        {
            var client = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);

            _notes = database.GetCollection<Note>(settings.BooksCollectionName);
        }

        public List<Note> Get() => _notes.Find(book => true).ToList();

        public Note Get(string id) => _notes.Find(book => book.Id == id).FirstOrDefault();

        public Note Create(Note book)
        {
            _notes.InsertOne(book);
            return book;
        }

        public void Update(string id, Note bookIn) =>
            _notes.ReplaceOne(book => book.Id == id, bookIn);

        public void Remove(Note bookIn) =>
            _notes.DeleteOne(book => book.Id == bookIn.Id);

        public void Remove(string id) =>
            _notes.DeleteOne(book => book.Id == id);
    }
}
